﻿using WebAssistant.WinUI.Properties;
using Sunny.UI;
using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace WebAssistant.WinUI
{
    public class UIInterFaceItem : UIControl
    {
        public UIInterFaceItem()
        {
            Width = 130;
            Height = 130;
            fillColor = System.Drawing.Color.White;
            BackColor = System.Drawing.Color.White;
            rectColor = System.Drawing.Color.Black;
            //rectColor = Sunny.UI.UIStyle.Blue;
            Style = Sunny.UI.UIStyle.Custom;
            StyleCustomMode = true;
            Cursor = Cursors.Arrow;
            UpdateStyles();

            //ToolTip
            ToolTip = new Sunny.UI.UIToolTip();


            //移除按钮
            RemoveButton = new UISymbolButton();
            RemoveButton.Cursor = System.Windows.Forms.Cursors.Hand;
            RemoveButton.FillColor = System.Drawing.Color.FromArgb(((int)(((byte)(230)))), ((int)(((byte)(80)))), ((int)(((byte)(80)))));
            RemoveButton.FillHoverColor = System.Drawing.Color.FromArgb(((int)(((byte)(232)))), ((int)(((byte)(127)))), ((int)(((byte)(128)))));
            RemoveButton.FillPressColor = System.Drawing.Color.FromArgb(((int)(((byte)(202)))), ((int)(((byte)(87)))), ((int)(((byte)(89)))));
            RemoveButton.FillSelectedColor = System.Drawing.Color.FromArgb(((int)(((byte)(202)))), ((int)(((byte)(87)))), ((int)(((byte)(89)))));
            RemoveButton.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
            RemoveButton.Location = new System.Drawing.Point(3, 3);
            RemoveButton.MinimumSize = new System.Drawing.Size(1, 1);;
            RemoveButton.Radius = 0;
            RemoveButton.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(230)))), ((int)(((byte)(80)))), ((int)(((byte)(80)))));
            RemoveButton.RectHoverColor = System.Drawing.Color.FromArgb(((int)(((byte)(232)))), ((int)(((byte)(127)))), ((int)(((byte)(128)))));
            RemoveButton.RectPressColor = System.Drawing.Color.FromArgb(((int)(((byte)(202)))), ((int)(((byte)(87)))), ((int)(((byte)(89)))));
            RemoveButton.RectSelectedColor = System.Drawing.Color.FromArgb(((int)(((byte)(202)))), ((int)(((byte)(87)))), ((int)(((byte)(89)))));
            RemoveButton.Size = new System.Drawing.Size(30, 30);
            RemoveButton.Style = Sunny.UI.UIStyle.Custom;
            RemoveButton.StyleCustomMode = true;
            RemoveButton.Symbol = 61453;
            RemoveButton.TabIndex = 3;
            RemoveButton.Visible = false;
            RemoveButton.Click += new System.EventHandler(this.RemoveClick);
            ToolTip.SetToolTip(RemoveButton, "删除接口服务");
            this.Controls.Add(RemoveButton);

            //设备图片
            DevicePic = new System.Windows.Forms.PictureBox();
            DevicePic.BackgroundImage = global::WebAssistant.WinUI.Properties.Resources.Printer;
            DevicePic.Cursor = System.Windows.Forms.Cursors.Arrow;
            DevicePic.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
            DevicePic.Location = new System.Drawing.Point(20, 10);
            DevicePic.Size = new System.Drawing.Size(90, 60);
            DevicePic.TabIndex = 1;
            DevicePic.TabStop = false;
            
            this.Controls.Add(DevicePic);

            //服务启动按钮
            SatrtButton = new UISymbolButton();
            SatrtButton.Cursor = System.Windows.Forms.Cursors.Hand;
            SatrtButton.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
            SatrtButton.Location = new System.Drawing.Point(20, 74);
            SatrtButton.MinimumSize = new System.Drawing.Size(1, 1);;
            SatrtButton.Radius = 0;
            SatrtButton.Size = new System.Drawing.Size(30, 30);
            SatrtButton.Style = Sunny.UI.UIStyle.Custom;
            SatrtButton.StyleCustomMode = true;
            SatrtButton.Symbol = 61515;
            SatrtButton.TabIndex = 4;
            SatrtButton.Click += new EventHandler(ClickChangeState);            
            this.Controls.Add(SatrtButton);

            //服务自动启动
            EnableServiceSwitch = new UISwitch();
            EnableServiceSwitch.ActiveColor = System.Drawing.Color.FromArgb(((int)(((byte)(110)))), ((int)(((byte)(190)))), ((int)(((byte)(40)))));
            EnableServiceSwitch.Font = new System.Drawing.Font("微软雅黑", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
            EnableServiceSwitch.Location = new System.Drawing.Point(50, 74);
            EnableServiceSwitch.MinimumSize = new System.Drawing.Size(1, 1);
            EnableServiceSwitch.Radius = 0;
            EnableServiceSwitch.RadiusSides = Sunny.UI.UICornerRadiusSides.None;
            EnableServiceSwitch.Size = new System.Drawing.Size(60, 30);
            EnableServiceSwitch.Style = Sunny.UI.UIStyle.Green;
            EnableServiceSwitch.StyleCustomMode = true;
            EnableServiceSwitch.SwitchShape = Sunny.UI.UISwitch.UISwitchShape.Square;
            EnableServiceSwitch.TabIndex = 5;
            EnableServiceSwitch.Click += new EventHandler(OnChangedServiceAutoEnable);
            this.Controls.Add(EnableServiceSwitch);

            //服务标题
            ServiceTitleLabel = new UILabel();            
            ServiceTitleLabel.BackColor = System.Drawing.Color.Transparent;
            ServiceTitleLabel.Dock = System.Windows.Forms.DockStyle.Bottom;
            ServiceTitleLabel.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
            ServiceTitleLabel.Location = new System.Drawing.Point(0, 105);;
            ServiceTitleLabel.Size = new System.Drawing.Size(130, 25);
            ServiceTitleLabel.Style = Sunny.UI.UIStyle.Custom;
            ServiceTitleLabel.TabIndex = 2;
            ServiceTitleLabel.Text = "打印服务";
            ServiceTitleLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            this.Controls.Add(ServiceTitleLabel);

        }

        private void EnableServiceSwitch_ValueChanged(object sender, bool value)
        {
            throw new NotImplementedException();
        }

        private Sunny.UI.UIToolTip ToolTip;
        private Sunny.UI.UISymbolButton RemoveButton;
        private Sunny.UI.UISymbolButton SatrtButton;
        private System.Windows.Forms.PictureBox DevicePic;
        private Sunny.UI.UISwitch EnableServiceSwitch;
        private Sunny.UI.UILabel ServiceTitleLabel;

        private string summary;
        public string Summary
        {
            get => summary;
            set {
                summary = value;
                ToolTip.SetToolTip(DevicePic, value, this.Name, 61530, 32, UIColor.Green);
            }
        }

        private string interfacekey;
        public string InterfaceKey
        {
            get => interfacekey;
            set
            {
                interfacekey = value;
            }
        }

        #region 移除服务

        private bool showRemoveButton;

        public bool ShowRemoveButton
        {
            get => ShowRemoveButton;
            set
            {
                if (showRemoveButton != value)
                {
                    showRemoveButton = value;
                }
            }
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (showRemoveButton)
            {
                //判断鼠标是否还在本控件的矩形区域内
                if (this.RectangleToScreen(this.ClientRectangle).Contains(Control.MousePosition))
                {
                    RemoveButton.Visible = true;
                }
                else
                {
                    RemoveButton.Visible = false;
                }
            }
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            if (showRemoveButton)
            {
                //判断鼠标是否还在本控件的矩形区域内
                if (this.RectangleToScreen(this.ClientRectangle).Contains(Control.MousePosition))
                {
                    RemoveButton.Visible = true;
                }
                else
                {
                    RemoveButton.Visible = false;
                }
            }            
        }

        private void RemoveClick(object sender, System.EventArgs e)
        {
            //base.RemoveClick(e);
            if (showRemoveButton)
            {
                if (OnClickRemove != null)
                {
                    OnClickRemove?.Invoke(sender, e);
                    //OnClickRemove(sender, new EventArgs());//把按钮自身作为参数传递
                }
            }
        }
        //定义点击删除委托
        public delegate void OnClickRemoveHandle(object sender, EventArgs e);

        //定义点击删除事件
        public event OnClickRemoveHandle OnClickRemove;
        #endregion

        #region 启动服务

        private bool state;

        [Description("是否启动")]
        public bool State
        {
            get => state;
            set
            {
                state = value;
                if (value)
                {
                    SatrtButton.Symbol = 61516;
                    SatrtButton.Style = UIStyle.Red;
                }
                else
                {
                    SatrtButton.Symbol = 61515;
                    SatrtButton.Style = UIStyle.Blue;
                }
                ToolTip.SetToolTip(SatrtButton, "接口服务状态：" + (value ? "已启动" : "未启动"));
            }
        }
        
        private void ClickChangeState(object sender, EventArgs e)
        {
            this.state = !this.state;
            if (this.state)
            {
                SatrtButton.Symbol = 61516;
                SatrtButton.Style = UIStyle.Red;
            }
            else
            {
                SatrtButton.Symbol = 61515;
                SatrtButton.Style = UIStyle.Blue;
            }            
            ToolTip.SetToolTip(SatrtButton, "接口服务状态：" + (this.state ? "已启动" : "未启动"));
            if (OnClickChangeStated != null)
            {
                OnClickChangeStated?.Invoke(sender, e);
            }
        }
        //定义点击变更服务状态事件委托
        public delegate void OnClickChangeStatedHandle(object sender, EventArgs e);

        //定义点击变更服务状态事件
        public event OnClickChangeStatedHandle OnClickChangeStated;
        #endregion

        #region 自动启动服务

        private bool autoStart;
        public bool AutoStart
        {
            get => autoStart;
            set
            {
                autoStart = value;
                EnableServiceSwitch.Active = value;
                ToolTip.SetToolTip(EnableServiceSwitch, "接口服务自动启动：" + (value ? "启用" : "关闭"));
            }
        }

        private void OnChangedServiceAutoEnable(object sender, EventArgs e)
        {
            UISwitch uiswitch = (UISwitch)sender;
            this.autoStart = uiswitch.Active;
            EnableServiceSwitch.Active = this.autoStart;
            ToolTip.SetToolTip(EnableServiceSwitch, "接口服务自动启动：" + (this.autoStart ? "启用" : "关闭"));
            if (OnSwitchState != null)
            {
                OnSwitchState?.Invoke(sender, e);
            }
        }

        //定义点击变更自动启动服务状态事件委托
        public delegate void OnSwitchStateHandle(object sender, EventArgs e);

        //定义点击变更自动启动服务状态事件
        public event OnSwitchStateHandle OnSwitchState;

        #endregion

        #region 接口类型
        private UIInterFaceType interFaceType;
        public UIInterFaceType InterFaceType
        {
            get => interFaceType;
            set
            {
                if (interFaceType != value)
                {
                    interFaceType = value;

                    if (interFaceType == UIInterFaceType.Printer)
                    {
                        DevicePic.BackgroundImage = Resources.Printer;
                        ServiceTitleLabel.Text = "打印服务";
                    }

                    if (interFaceType == UIInterFaceType.IDCard)
                    {
                        DevicePic.BackgroundImage = Resources.IDCard;
                        ServiceTitleLabel.Text = "身份证";
                    }

                }
            }
        }

        public enum UIInterFaceType
        {
            /// <summary>
            /// 打印机
            /// </summary>
            [DisplayText("Printer")]
            Printer = 0,

            /// <summary>
            /// 身份证
            /// </summary>
            [DisplayText("IDCard")]
            IDCard = 1,
        }
        #endregion

       
    }
}
